//
//  AY_RankCell.m
//  AiYue
//
//  Created by mac on 2023/10/23.
//

#import "AY_RankCell.h"

@interface AY_RankCell ()

@property (nonatomic, strong) UIImageView *icon;
@property (nonatomic, strong) UILabel *indexLabel;
@property (nonatomic, strong) UIImageView *bookImg;
@property (nonatomic, strong) UILabel *bookName;
@property (nonatomic, strong) UILabel *bookDesc;
@property (nonatomic, strong) UILabel *bookChapter;
@property (strong,nonatomic) UILabel *tipLabel;

@end

@implementation AY_RankCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self)
    {
        self.icon = [AYTool createImageViewWithFrame:CGRectZero andImage:nil andBg:nil andRadius:0 andAddSubView:self.contentView];
        [self.icon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(YNUM(12));
            make.centerY.equalTo(self.contentView);
            make.width.mas_equalTo(YNUM(26));
            make.height.mas_equalTo(YNUM(30));
        }];
        
        self.indexLabel = [AYTool createLabelWithFrame:CGRectZero andText:nil andTextColor:[UIColor whiteColor] andBgColor:nil andFont:[UIFont boldSystemFontOfSize:YNUM(14)] andLines:1 andRadius:0 andAddSubView:self.icon];
        [self.indexLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.icon);
            make.centerY.equalTo(self.icon).offset(YNUM(3));
        }];
        
        self.bookImg = [AYTool createImageViewWithFrame:CGRectZero andImage:nil andBg:nil andRadius:YNUM(4) andAddSubView:self.contentView];
        [self.bookImg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.icon.mas_right).offset(YNUM(12));
            make.top.equalTo(self.contentView).offset(YNUM(15));
            make.width.mas_equalTo(YNUM(74));
            make.height.mas_equalTo(YNUM(100));
        }];
        
        self.tipLabel = [AYTool createLabelWithFrame:CGRectZero andText:nil andTextColor:[UIColor whiteColor] andBgColor:nil andFont:YFont(10) andLines:1 andRadius:0 andAddSubView:self.bookImg];
        self.tipLabel.layer.masksToBounds = NO;
        self.tipLabel.textAlignment = NSTextAlignmentCenter;
        [self.tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.top.equalTo(self.bookImg);
            make.width.mas_equalTo(YNUM(28));
            make.height.mas_equalTo(YNUM(16));
        }];
        
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, YNUM(28), YNUM(16)) byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(YNUM(4), YNUM(4))];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = self.tipLabel.bounds;
        maskLayer.path = maskPath.CGPath;
        self.tipLabel.layer.mask = maskLayer;
        
        self.bookName = [AYTool createLabelWithFrame:CGRectZero andText:nil andTextColor:COLOR(@"010101") andBgColor:nil andFont:[UIFont boldSystemFontOfSize:YNUM(16)] andLines:1 andRadius:0 andAddSubView:self.contentView];
        [self.bookName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.bookImg.mas_right).offset(YNUM(12));
            make.right.equalTo(self.contentView).offset(-YNUM(12));
            make.top.equalTo(self.contentView).offset(YNUM(20));
        }];
        
        self.bookDesc = [AYTool createLabelWithFrame:CGRectZero andText:nil andTextColor:COLOR(@"#999999") andBgColor:nil andFont:YFont(12) andLines:2 andRadius:0 andAddSubView:self.contentView];
        [self.bookDesc mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.bookName);
            make.top.equalTo(self.bookName.mas_bottom).offset(YNUM(8));
        }];
        
        self.bookChapter = [AYTool createLabelWithFrame:CGRectZero andText:nil andTextColor:COLOR(@"#999999") andBgColor:nil andFont:YFont(12) andLines:1 andRadius:0 andAddSubView:self.contentView];
        [self.bookChapter mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.bookName);
            make.bottom.equalTo(self.bookImg).offset(-YNUM(5));
        }];
    }
    return self;
}

- (void)sendBookModel:(RankListModel *)model andIndex:(NSInteger)index {
    NSString *image = index > 3 ? @"AY_rank_4" : [NSString stringWithFormat:@"AY_rank_%lu",index + 1];
    self.icon.image = YImage(image);
    self.indexLabel.text = [NSString stringWithFormat:@"%lu",index + 1];
    [self.bookImg sd_setImageWithURL:[NSURL URLWithString:model.image] placeholderImage:YImage(@"placeholderImage")];
    self.bookName.text = model.name;
    self.bookDesc.text = model.info;
    self.bookChapter.text = [NSString stringWithFormat:@"%@ · %@ · %@章",model.author,model.categoryName,model.chapterNum];
    [AYTool setBookStatusWithStatus:model.status andLabel:self.tipLabel];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
